package com.loopy.fruns.dao.impl;

import com.loopy.fruns.dao.BasicDAO;
import com.loopy.fruns.dao.FurnDAO;
import com.loopy.fruns.entity.Furn;
import com.loopy.fruns.entity.PageVO;

import java.util.List;

public class FurnDAOImpl extends BasicDAO<Furn> implements FurnDAO {

    @Override
    public List<Furn> queryFurns() {
        String sql="select `id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path` `imgPath` from furn";
        List<Furn> list = Multi(sql, Furn.class);
        return list;
    }


    @Override
    public int addFurn(Furn furn) {
        String sql="insert into `furn`(`id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path`) values(null,?,?,?,?,?,?)";
        int update = update(sql, furn.getName(), furn.getMaker(), furn.getPrice(), furn.getSales(), furn.getStock(), furn.getImgPath());
        return update;
    }

    @Override
    public int deleteFurnById(Integer id) {
        String sql="delete from `furn` where `id`=?";
        int update = update(sql, id);
        return update;
    }

    @Override
    public Furn  queryFurnById(Integer id) {
        String sql="select `id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path` `imgPath` from `furn` where `id`=?";
        Furn furn = QuerySingle(sql, Furn.class, id);
        return furn;
    }

    @Override
    public int updateFurn(Furn furn) {
        String sql="update `furn` set `name`= ? ,`maker`= ? , `price`= ? ,`sales`= ? ,`stock`= ? ,`img_path`= ? where id= ? ";
        int update = update(sql, furn.getName(), furn.getMaker(), furn.getPrice(), furn.getSales(), furn.getStock(), furn.getImgPath(),furn.getId());
        return update;
    }

    @Override
    public List<Furn> getPageItem(int begin,int pageSize) {
        String sql="select `id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path` `imgPath` from `furn` limit ? , ?";
        return Multi(sql,Furn.class,begin,pageSize);
    }

    @Override
    public int getTotalRow() {
        String sql="SELECT count(*) from `furn`";

        return ((Number)QueryScalar(sql)).intValue();
    }

    @Override
    public int getTotalRowByName(String name) {
        String sql="SELECT count(*) from `furn` where `name` like ?";
        return ((Number)QueryScalar(sql,"%"+name+"%")).intValue();
    }

    @Override
    public List<Furn> getPageItemByName(String name,int begin,int pageSize) {
        String sql="select `id`,`name`,`maker`,`price`,`sales`,`stock`,`img_path` `imgPath` from `furn` where name like ? limit ? , ? ";
        return Multi(sql,Furn.class,"%"+name+"%",begin,pageSize);
    }

}
